/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * this is the section used to display the type of aggregation
 */
public class AggregationSection extends PropertyViewSection{



	/**
	 * the combo that display the type of aggregation
	 */
	private CCombo aggregationKindCombo    = null;

	/**
	 *  3 types of aggregation
	 */
	protected AggregationKind[] aggregationKinds = new AggregationKind[3];

	/**
	 * listener to update the property view
	 */
	protected SelectionListener aggregationListener;

	/**
	 * {@inheritDoc}
	 */
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		aggregationKinds[0]=AggregationKind.NONE_LITERAL;
		aggregationKinds[1]=AggregationKind.SHARED_LITERAL;
		aggregationKinds[2]=AggregationKind.COMPOSITE_LITERAL;

		CLabel l = getWidgetFactory().createCLabel(composite, "Aggreg. kind:"); //$NON-NLS-1$
		l.setToolTipText("Aggregation kind");

		aggregationKindCombo = getWidgetFactory().createCCombo(composite, SWT.NONE);
		aggregationKindCombo.setEditable(false);
		aggregationKindCombo.add("Simple");
		aggregationKindCombo.add("Aggregation");
		aggregationKindCombo.add("Composition");
		data = new FormData(100,15);
		data.left = new FormAttachment(1, STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		aggregationKindCombo.setLayoutData(data);

//		Add listener on combo
		aggregationKindCombo.addSelectionListener(
				aggregationListener = new SelectionListener() {
					public void widgetSelected(SelectionEvent e) {

						((Property)getElement()).setAggregation(AggregationKind.get(aggregationKindCombo.getSelectionIndex()));
					}

					public void widgetDefaultSelected(SelectionEvent e){
					}
				});
	}


	/**
	 * {@inheritDoc}
	 */
	public void refresh() {
		if(!aggregationKindCombo.isDisposed()){
			if (((Property)getElement()).getAggregation()!=null){
				for( int i=0; i<aggregationKinds.length;i++){
					if( aggregationKinds[i].equals(((Property)getElement()).getAggregation())){
						aggregationKindCombo.select(i);
					}
				}
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */	public void dispose(){
		 super.dispose();
		 if(aggregationKindCombo!=null && !aggregationKindCombo.isDisposed())
			 aggregationKindCombo.removeSelectionListener(aggregationListener);
	 }
}
